import request from '../../request/index'
const state = {
    orderData: [], //总数据
    filterData: [], //筛选过后的数据
    searchData: [], //搜索过后的数据
}
const getters = {

}
const mutations = {
    // 获取订单数据
    setOrderData(state, res) {
        state.orderData = res
    },

    // 筛选订单数据
    filterOrderData(state, res) {
        state.filterData = state.orderData.filter(item => {
            if (res === "0") {
                return item
            }
            return item.orderStatus === res
        })
    },

    // 搜索订单数据
    searchOrderData(state, res) {
        let {
            time,
            selVal,
            name,
            phone
        } = res
        let newtime = time.toLocaleDateString()
        state.filterData = state.filterData.length === 0
        ? state.orderData.filter(item => {
            if (selVal === "0") {
                return `${item.year}/${item.month}/${item.day}` === newtime && item.name === name && item.phoneNum === phone
            }else if (selVal !== "0") {
                return `${item.year}/${item.month}/${item.day}` === newtime && item.orderStatus === selVal && item.name === name && item.phoneNum === phone
            }
            
            
            // else if (item.name === name) {
            //         return item
            //     } else if (`${item.year}/${item.month}` === time) {
            //         return item
            //     } else if (item.orderStatus === selVal) {
            //         return item
            //     } else if (item.phoneNum === phone) {
            //         return item
            //     }
        })
        : state.filterData.filter(item => {
            if (selVal === "0") {
                return `${item.year}/${item.month}/${item.day}` === newtime && item.name === name && item.phoneNum === phone
            }else if (selVal !== "0") {
                return `${item.year}/${item.month}/${item.day}` === newtime && item.orderStatus === selVal && item.name === name && item.phoneNum === phone
            }
            
            //  else if (item.name === name) {
            //         return item
            //     } else if (`${item.year}/${item.month}` === time) {
            //         return item
            //     } else if (item.orderStatus === selVal) {
            //         return item
            //     } else if (item.phoneNum === phone) {
            //         return item
            //     }
        })
    },
    // 重置数据
    resetData(state) {
        state.filterData = state.orderData
    },

    // 升序
    ascendingOrder(state,res){
        console.log(res);
        if(res === "ordermoney"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return a.ordermoney - b.ordermoney}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return a.ordermoney - b.ordermoney}) :state.orderData.sort((a,b) => {return a.ordermoney - b.ordermoney}))
        }else if(res === "paymoney"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return a.paymoney - b.paymoney}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return a.paymoney - b.paymoney}) :state.orderData.sort((a,b) => {return a.paymoney - b.paymoney}))
        }else if(res === "downTime"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return a.month - b.month}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return a.month - b.month}) :state.orderData.sort((a,b) => {return a.month - b.month}))
        }
    },
    // 降序
    descendingOrder(state,res){
        console.log(res)
        if(res === "ordermoney"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return b.ordermoney - a.ordermoney}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return b.ordermoney - a.ordermoney}) :state.orderData.sort((a,b) => {return b.ordermoney - a.ordermoney}))
        }else if(res === "paymoney"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return b.paymoney - a.paymoney}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return b.paymoney - a.paymoney}) :state.orderData.sort((a,b) => {return b.paymoney - a.paymoney}))
        }else if(res === "downTime"){
            state.filterData.length !== 0 ? state.filterData.sort((a,b) => {return b.month - a.month}):(state.searchData.length !== 0 ? state.searchData.sort((a,b) => {return b.month - a.month}) :state.orderData.sort((a,b) => {return b.month - a.month}))
        }
    },
}
const actions = {
    // 获取订单数据
    async getOrderData(context) {
        await request({
            url: "/getOrderList",
            method: "GET",
            params: {},
        }).then(res => {
            context.commit('setOrderData', res.data)
        })
    }
}
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}